<?php

namespace App\Http\Controllers\backend;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Redirect;
use App\Models\Member;
use Illuminate\Support\Facades\Hash;
use App\Models\Operation;

class PasswordController extends BaseController {
	
	public function disp() {
		return View::make('backend.password_reset');
	}
	
	public function reset() {
		$rules = [
			'oPassword' => 'required',
			'password' => 'required',
			'password2' => 'required|same:password',
		];
		$msgs = [
			'oPassword.required' => '旧密码不能为空。',
			'password.required' => '新密码不能为空。',
			'password2.required' => '确认密码不能为空。',
			'password2.same' => '两次密码不一致。',
		];
		$validation = Validator::make(Input::all(), $rules, $msgs);
		if($validation->passes()) {
			$member = Member::find(Auth::id());
			if(!Hash::check(Input::get('oPassword'), $member->password)) {
				Session::flash('msg', '旧密码不正确。');
				return Redirect::back();
			}
			$member->password = Hash::make(Input::get('password'));
			$member->save();
			Operation::create([
				'action' => '修改密码',
				'content' => '修改密码成功',
			]);
			return Redirect::to('/backend/');
		}
		return Redirect::back()
			->withInput()
			->withErrors($validation);
	}
}
